﻿/*
#-------------------------------------------------------------------------------
# Name:        XMLProvider - Wrapper for the .NET XmlDocument
#
# Usage:       Pass a filepath to open the XML Document
#              Then you can navigate through the XML file and edit it.
#
#
# Requires:    Visual Studio 2010 and up.
#
# Author:      Roy Scheefhals
#
# Created:     26-03-2012
#-------------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Xml.XPath;

namespace LibraryCS
{
    /// <summary>
    /// This class provides easy XML access.
    /// </summary>
    public class XMLProvider
    {
        private FileInfo fileinfo;
        private XmlDocument _myXmlDocument;

        private List<XmlNode> NodeLayers = new List<XmlNode>();

        public XmlNode rootNode;
        public XmlNode activeNode;
        /// <summary>
        /// The XML contents of this XMLProvider Object
        /// </summary>
        public XmlDocument Document
        {
            get
            {
                if (_myXmlDocument.DocumentElement != null)
                {
                    return _myXmlDocument;
                }
                throw new FileLoadException();
            }
        }

        /// <summary>
        /// Constructor for the XMLProvider Class. Opens an existing XML file.
        /// </summary>
        /// <param name="f">file to open</param>
        /// 

        public XMLProvider(FileInfo f)
        {
            if (f.Extension != ".xml")
            {
                throw new Exception();
            }
            else
            {
                fileinfo = f;
                _myXmlDocument = new XmlDocument();
                try
                {
                    _myXmlDocument.Load(f.ToString());
                }
                catch (XmlException xex)
                {
                    Debug.WriteLine(xex.Message);
                    throw new FileNotFoundException("Error with the format of the XML file");
                }
                rootNode = _myXmlDocument.DocumentElement;
                activeNode = rootNode;
            }
        }

        /// <summary>
        /// Constructor for XMLProvider Class. Creates a new XML file.
        /// </summary>
        /// <param name="f">Fileinfo linking to the new XML file</param>
        /// <param name="RootNode"></param>
        public XMLProvider(FileInfo f, String RootNodeName)
        {
            if (f.Extension != ".xml")
            {
                throw new Exception();
            }
            fileinfo = f;
            _myXmlDocument = new XmlDocument();
            XmlDeclaration dec = _myXmlDocument.CreateXmlDeclaration("1.0", null, null);
            _myXmlDocument.AppendChild(dec);
            _myXmlDocument.AppendChild(_myXmlDocument.CreateElement(RootNodeName));

            rootNode = _myXmlDocument.DocumentElement;
            activeNode = rootNode;

        }

        public XmlNode getFirstNode()
        {
            activeNode = rootNode.FirstChild;
            return activeNode;
        }

        public Boolean nodeLayerUp()
        {
            if (activeNode.ParentNode != null)
            {
                activeNode = activeNode.ParentNode;
                return true;
            }
            return false;
        }

        public Boolean nodeLayerDown()
        {
            if (activeNode.HasChildNodes)
            {
                activeNode = activeNode.FirstChild;
                return true;
            }
            return false;

        }

        public Boolean nextSibling()
        {
            if (activeNode.NextSibling != null)
            {
                activeNode = activeNode.NextSibling;
                return true;
            }
            return false;
        }

        public Boolean previousSibling()
        {
            if (activeNode.PreviousSibling != null)
            {
                activeNode = activeNode.PreviousSibling;
                return true;
            }
            return false;
        }

        #region WriteMethods
        public void addNodeToCurrentLayer(XmlNode newNode)
        {
            activeNode.AppendChild(newNode);
        }

        public void addNodeLayer(XmlNode newNodeLayer)
        {
            activeNode.AppendChild(newNodeLayer);
            activeNode = newNodeLayer;
        }

        public void saveFile(String path)
        {
            _myXmlDocument.Save(path);
        }

        public void saveFile()
        {
            _myXmlDocument.Save(fileinfo.ToString());
        }
        #endregion
    }
}